package com.subway.babynote;
/*水*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import com.subway.babynote.syncdata.ClientDataRecord.feedRecord;
import com.subway.babynote.syncservice.syncapi;
import java.util.Date;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TextView;

public class inputtime extends Activity {
	private static final int INVISIBLE = 0x00000004;
	static final int RG_AMOUNT = 6;
	private int my_Year;
	private int my_Month;
	private int my_Day;
	private int my_Hour;
	private int my_Minute;
	TextView previous_view;
	TextView food_view;
	TimePicker my_timePicker;
	Calendar my_Calendar;
	Button login_button;
	private Button confirm_button;
	NotedbHelper notedb;
	int food_id;
	int user_id;
	Bundle bundle;
	Date date;
	int r_date;
	String r_date_view;
	int r_time;
	String r_time_view;
	private LinearLayout linearLayout, statistics, sync, more;
	Intent In;

	public void onCreate(Bundle savedInstanceState) {

		//sa = syncapi.getSyncImpl();
		/* 从Calendar抽象基类获得实例对象，并设置成中国时区 */
		my_Calendar = Calendar.getInstance(Locale.CHINA);
		/* 从日历对象中获取当前的：年、月、日、时、分 */
		my_Year = my_Calendar.get(Calendar.YEAR);
		my_Month = my_Calendar.get(Calendar.MONTH) ;
		my_Day = my_Calendar.get(Calendar.DAY_OF_MONTH);
		my_Hour = my_Calendar.get(Calendar.HOUR_OF_DAY);
		my_Minute = my_Calendar.get(Calendar.MINUTE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputtime);
		setTitle(R.string.note);
		In = new Intent();
	//	initTabFunction();
		login_button = (Button) findViewById(R.id.login_button);
		my_timePicker = (TimePicker) findViewById(R.id.my_TimePicker);
		confirm_button = (Button) findViewById(R.id.confirm_button);
		previous_view = (TextView) findViewById(R.id.inputamount_view);
		food_view = (TextView) findViewById(R.id.noted_view);
		notedb = new NotedbHelper(this);
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			user_id = syncapi.getSyncImpl().getUserid();
			if (user_id == -1) {
				user_id = notedb.getVisitorid();
			}
			
		} catch (Exception e) {
			Log.v("inputtime", "get info from DB e=" + e.getMessage());
		}
		
		bundle = this.getIntent().getExtras();
		previous_view.setText(bundle.getString("amount")
				+ bundle.getString("unit"));
		food_view.setText(bundle.getString("foodname"));
		my_timePicker.setIs24HourView(true);
		my_timePicker.setCurrentHour(Integer.valueOf(my_Hour));
		my_timePicker.setCurrentMinute(Integer.valueOf(my_Minute));
		my_timePicker
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {

					}
				});
		confirm_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				r_date_view = Integer.toString(my_Year) + getResources().getString(R.string.year)
						+ Integer.toString(my_Month+1) + getResources().getString(R.string.month)
						+ Integer.toString(my_Day) + getResources().getString(R.string.day);
				date = new GregorianCalendar(my_Year, my_Month, my_Day, 0, 0)
						.getTime();
				r_date = (int) (date.getTime() / 1000);
				// r_date=Integer.toString(my_Year)+"-"+Integer.toString(my_Month)+"-"+Integer.toString(my_Day);
				r_time_view = Integer.toString(my_Hour) + getResources().getString(R.string.hour)
						+ Integer.toString(my_Minute) + getResources().getString(R.string.minute);
				date = new GregorianCalendar(my_Year, my_Month, my_Day,
						my_Hour, my_Minute).getTime();
				r_time = (int) (date.getTime() / 1000);
				
				// insert to db and call sync;
				try {
				/*	notedb.AddFeed(bundle.getInt("food_id"), user_id, r_date,
						r_time, bundle.getFloat("quantity"));

				//	int ret = syncapi.getSyncImpl().informSync(
							syncapi.operation_type.addFeedRecord.code,
							notedb.nowTime());*/
					
					feedRecord fr=new feedRecord();
					fr.client_id=user_id;
					fr.food_id=bundle.getInt("food_id");
					fr.quantity=bundle.getFloat("quantity");
					fr.r_date=r_date;
					fr.r_time=r_time;
					int ret=syncapi.getSyncImpl().AddFeed(fr);
					if (ret != 0) {
						Log.v("feed", "sync to server failed");
					}
					
				
				} catch (Exception e) {
					Log.v("addfeedrecord", e.getMessage());
				}
				bundle.putString("timestamp", r_date_view + r_time_view);
				bundle.putString("amount", previous_view.getText().toString());
				bundle.putString("foodname", food_view.getText().toString());

				In.setClass(inputtime.this, notefinish.class);
				In.putExtras(bundle);
				//
			    setResult(RESULT_OK,In);
			    startActivity(In);
				inputtime.this.finish();
				
			}

		});

		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent In = new Intent();
				In.setClass(inputtime.this, login.class);
				startActivity(In);
			}
		});
	}



	public void onDestroy() {
		
		notedb.close();
		super.onDestroy();
	}
}